package com.boe.config.service;


import cn.hutool.json.JSONUtil;
import com.boe.common.mvc.LoginUserModel;
import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import lombok.RequiredArgsConstructor;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
public class LoginUserService {
    @Value("${app.product-line.code:}")
    String productLineCode;

    @NotNull
    StringRedisTemplate redisTemplate;


    public LoginUserModel getLoginUserInfo(String token) {
        if (Strings.isNullOrEmpty(token)) {
            return null;
        }

        var keys = Lists.newArrayList(productLineCode, token);
        keys.removeIf(Strings::isNullOrEmpty);

        //查询 Redis
        var tokenValue = redisTemplate.opsForValue().get(String.join(":", keys));
        if (Strings.isNullOrEmpty(tokenValue)) {
            return null;
        }

        return JSONUtil.toBean(tokenValue, LoginUserModel.class);
    }
}
